//
//  ZHelpController.m
//  Zlottery
//
//  Created by HuaYingJiTuan on 16/1/12.
//  Copyright © 2016年 张庆庆. All rights reserved.
//

#import "ZHelpController.h"
#import "ZHelp.h"
#import "ZHelpContentController.h"
@interface ZHelpController ()
@property (nonatomic, strong) NSArray *helps;
@property (nonatomic, strong) NSIndexPath *indexPath;

@end

@implementation ZHelpController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"常见问题";
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, -1)];
}
//加载json文件中的数据
- (NSArray *)helps{
    if (_helps == nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"help.json" ofType:nil];
        NSData *data = [NSData dataWithContentsOfFile:path];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        
        //字典转模型
        NSMutableArray *mArray = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            ZHelp *help = [ZHelp helpWithDic:dict];
            [mArray addObject:help];
        }
        _helps = [mArray copy];
    }
    return _helps;
}
//2 把czhelp的数据转换成czitem
- (void)loadGroups{
    //
    NSMutableArray *mArray = [NSMutableArray array];
    for (ZHelp *help in self.helps) {
        ZItem *item = [ZItemArrow itemWithTitle:help.title icon:nil option:^{
            //弹出content的控制器，显示网页内容
            [self loadContent];
        }];
        [mArray addObject:item];
    }
    ZGroup *group = [ZGroup groupWithItems:[mArray copy]];
    
    self.groups = @[group];
}
//加载contentController
- (void)loadContent{
    //获取模型数据
    ZHelp *help = self.helps[self.indexPath.row];
    
    ZHelpContentController *vc = [ZHelpContentController new];
    
    UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:vc];
    //设置标题
    vc.title = help.title;
    vc.help = help;
    
    //设置关闭按钮
    vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
    
    //弹出控制器
    [self presentViewController:navC animated:YES completion:nil];
}
//关闭弹出的控制器
- (void)close{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//点击cell的时候执行option  跳转到下一个控制器
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.indexPath = indexPath;
    ZGroup *group = self.groups[self.indexPath.section];
    ZItem *item = group.items[self.indexPath.row];
    
    if (item.option) {
        item.option();
    }
    
}

@end
